class Test implements Runnable {
    private int t = 20;

    public void run() {
        String name = Thread.currentThread().getName();
        int m = (int)(Math.random() * 900 + 100);
        while (true) {
            try {
                Thread.sleep(m);
            } catch (Exception e) {}

            synchronized (this) {
                if (t > 0) {
                    System.out.printf("%s 出售一张票 %d, 时间 %d 毫秒\n", name, t--, m);
                } else {
                    break;
                }
            }
        }
    }
}

public class RunnableTest {
    static public void main(String [] args) {
        Test t = new Test();
        new Thread(t, "售票口1").start();
        (new Thread(t, "售票口2")).start();
        (new Thread(t, "售票口3")).start();
        (new Thread(t, "售票口4")).start();

        /*
         * int s = 1;
         * while (s < 100) {
         *     System.out.printf("Main run %d\n", s++);
         * }
         */
    }
}
